<?php
@set_time_limit(1200);
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : Char.class.php   FILE_PATH : classes\libraries\Char.class.php
 * ....多级分类字符串处理类
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Tue Aug 07 17:14:20 CST 2007
 */

/*
+------------------------------------------------------------------------------------------------------
|   功能说明： 根据类表生成树型图.
|   调用部分 ：
|   ========================================
|   $test_array = get_page_children($node_cache,0, $node_cache,0);
|
|    echo "\n<select name=select>\n";
|
|    $last = "├─";
|    foreach($test_array as $key=>$val)
|   {
|      $item = str_repeat(" │ ",$val['depth']);
|      echo "<option value=".$val['id'].">".$item.$last.$val['name']."</option>\n";
|      $item = '';
|    }
|
+------------------------------------------------------------------------------------------------------
*/


class CategoryView {

	var $node_cache = "";

	function  node_cache($inputnode){
		global $node_cache;
		if (!is_array($node_cache) || empty($node_cache)){
			$this->node_cache = $inputnode;
		}
	}

	function &get_page_children($id,$node,$depth,$last,$repeat)
	{


		$this->node_cache = $node;
		$depth++;
		$tree_list = array();
		if (is_array($node)){
			foreach ($node as $leap )
			{
				if ($leap['parentId'] == $id)
				{
					$leap['depth'] = $depth - 1;
					$tree_list[] = $leap;
					if ( $children = $this->get_page_children($leap['id'], $node,$depth,$last,$repeat))
					{
						$tree_list = array_merge($tree_list, $children);
					}
				}
			}
		}
		$i = 0 ;
		foreach($tree_list as $key=>$val) {
			$item = str_repeat($repeat,$val['depth']);
			$treeListArray[$i][id]       = $val[id];
			$treeListArray[$i][name]     = $item.$last.$val[name];
			$treeListArray[$i][parentId]     = $val[parentId];
			$treeListArray[$i][sort]     = $val[sort];
			$i++;
		}
		return $treeListArray;
	}
	/**
	 * 获得指定ID的商品分类
	 *
	 * @param int $id
	 * @return array
	 */
	function &get_category_row($id, $node)
	{
		$this->node_cache = $node;
		$tree_list = array();
		if (is_array($node)){
			foreach ($node as $leap )
			{
				if ($leap['id'] == $id)
				{
					$tree_list[] = $leap;

				}
			}
		}
		return $tree_list;
	}
	/**
	 * 获得指定父ID下的指定深度的分类数组
	 *
	 * @param int $id
	 * @param array $node
	 * @param int $depth
	 * @return array $treeListArray
	 */
	function &get_children_array($id, $node, $depth, $deep="")
	{

		$this->node_cache = $node;
		$depth++;
		$tree_list = array();
		if (is_array($node)){
			foreach ($node as $leap )
			{

				if ($leap['parentId'] == $id)
				{
					if("" != $deep){

						if($deep > $depth-1){
							$tree_list[] = $leap;
						}

						elseif($deep = $depth-1){

							$next_tree_list[0] = $leap;

							if($next_tree_list[0]){
								return true;
							}else{
								return false;
							}
						}

						$last_key = count($tree_list)-1;
						if($deep >= $depth-1){
							if ($children = $this->get_children_array($leap['id'], $node, $depth, $deep))
							{
								$tree_list[$last_key][haveson]=1;
								if(is_array($children)){
									$tree_list = array_merge($tree_list, $children);
								}

							}
						}
					}else{
						if ($leap['parentId'] == $id)
						{
							$tree_list[] = $leap;
							$last_key = count($tree_list)-1;
							if ($children = $this->get_children_array($leap['id'], $node, $depth, $deep))
							{
								$tree_list[$last_key][haveson]=1;
								$tree_list = array_merge($tree_list, $children);
							}
						}
					}


				}

			}

		}
		$i = 0 ;
		foreach($tree_list as $key=>$val) {

			$treeListArray[$i][id]       = $val[id];
			$treeListArray[$i][name]     = $val[name];
			$treeListArray[$i][parentId]     = $val[parentId];
			$treeListArray[$i][haveson]     = $val[haveson];
			$i++;
		}

		return $treeListArray;
	}


	/*
	+------------------------------------------------------------------------------------------------------
	|   功能说明： 利用根据类表生成树型图的函数，生成一个下拉列表.
	|
	+------------------------------------------------------------------------------------------------------
	*/

	function get_page_select($Selectname,$Id,$Ex)
	{
		global $SelectFirst,$node_cache;
		$Array = $this->get_page_children(0, $node_cache,0);
		$Table = "";
		$Table .=" <select id='".$Selectname."' name='".$Selectname."' ".$Ex.">\n";
		$Table .=" <option value=0>├─".$SelectFirst[PleaseSelectFirst]."</option>\n";


		$last = "├─";
		foreach($Array as $key=>$val)
		{
			$item = str_repeat(" │ ",$val['depth']);
			$Table .= "<option value='".$val['id']."'";
			if (intval($Id)==intval($val['id'])){
				$Table .= " selected ";
			}
			$Table .= ">".$item.$last.$val['name']."</option>\n";
			$item = '';
		}
		$Table .= "</select>";
		return $Table;
	}

}

?>